package com.jxsd.edu.cn.struts2.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jxsd.edu.cn.struts2.models.Auth;
import com.jxsd.edu.cn.struts2.models.Category;
import com.jxsd.edu.cn.struts2.models.Loginview;
import com.jxsd.edu.cn.struts2.serviceImpl.CatagoryServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.UserServiceImpl;
import com.jxsd.edu.cn.struts2.services.CatagoryService;
import com.jxsd.edu.cn.struts2.services.UserService;
import com.opensymphony.xwork2.ActionContext;

public class LognRegAction  
{
     private     UserService  userService=new  UserServiceImpl();
     
    //一级类别信息
  	private     List<Category>   list;
    //IndexService
    private     CatagoryService   cataService;
     
     private     Loginview  login;
     private     Auth       auth;
     private     String    code;
     
     
     
     public  LognRegAction()
     {
    	 cataService=new  CatagoryServiceImpl();
     }
     
     
     /**
      * hander  the  request  for  login
      * */
     public  String  doLogin()
     {
    	 Loginview  dbview=null;
    	 
    	 String  c=(String)ActionContext.getContext().getSession().get("rand");
    	 
    	 if(!code.equals(c))
    	 {
    		 return  "login"; 
    	 }
    	 
    	 dbview=userService.doLogin(login);
    	 HttpServletRequest  request=ServletActionContext.getRequest();
    	 
    	 if(null==dbview)
    	 {
    		 request.setAttribute("error", "用户名或密码错误");
    		 return  "login";
    	 }
    	 dbview.setUserpassword("");
    	 //将用户信息保存到Session中
    	 ActionContext.getContext().getSession().put("nickName", dbview.getNickName());
    	 ActionContext.getContext().getSession().put("userId", dbview.getUserId());
    	 return  "dologin";
     }
     
     
     
     /**
      * hander  the  request  for  register
      * */
     public  String  doRegister()
     {
    	 String  result=userService.doRegister(auth);
    	
    	 return  "success";
     }
     
     /**
      * query  the  page  of  login
      * */
     public  String  queryLoginPage()
     {
    	 list=cataService.getAllCatagory();
    	 return  "login";
     }
     
     /**
      * hander  the  request  for  active
      * */
     public  String  doActive()
     {
    	 return  userService.doActiveUser(auth);
     }
     /**
      * hander  the  request  for  logout
      * */
     public   String  doLogout()
     {
    	 ActionContext.getContext().getSession().put("userId", null);
    	 ActionContext.getContext().getSession().put("nickName", null);
    	 ActionContext.getContext().getSession().clear();
    	 list=cataService.getAllCatagory();
    	 return  "login";
     }
     
     
     
	public Loginview getLogin() {
		return login;
	}
	public void setLogin(Loginview login) {
		this.login = login;
	}
	public Auth getAuth() {
		return auth;
	}
	public void setAuth(Auth auth) {
		this.auth = auth;
	}



	public List<Category> getList() {
		return list;
	}



	public void setList(List<Category> list) {
		this.list = list;
	}


	public String getCode() {
		return code;
	}


	public void setCode(String code) {
		this.code = code;
	}

}
